# 1、读取单词数据
with open("../../data/words.txt", mode="r", encoding="utf-8") as file:
    lines = [line.rstrip() for line in file.readlines()]

# 2、将一行中的多个单词拆分成多行

words = []

for line in lines:
    # 将一行拆分成多个单词
    split = line.split(",")

    # 循环将单词保存到列表中
    for word in split:
        words.append(word)

# 3、统计单词的数量

word_count = {}

for word in words:
    # 判断单词在字典中是否存在
    # 不存在存入一个1.存在加1
    if word not in word_count:
        word_count[word] = 1
    else:
        word_count[word] = word_count[word] + 1

print(word_count)

# 3、将结果保存到文件

with open("../../data/word_count.txt", mode="w", encoding="UTF-8") as file:
    for word, num in word_count.items():
        file.write("{}\t{}\n".format(word, num))
